package com.youlu.campus.service.form.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.form.SingleFormAnswer;
import com.youlu.campus.service.form.SingleFormAnswerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SingleFormAnswerServiceImpl implements SingleFormAnswerService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<SingleFormAnswer> findBySingleFormQuestionId(String singleFormQuestionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("singleFormQuestionId").is(singleFormQuestionId));
        Sort sort = Sort.by(Sort.Direction.DESC, "no");
        query.addCriteria(Criteria.where("deleted").ne(Boolean.TRUE));
        query.with(sort);
        return mongoTemplate.find(query, SingleFormAnswer.class);
    }

    @Override
    public SingleFormAnswer create(SingleFormAnswer singleFormAnswer) {
        singleFormAnswer.setId(null);
        if (Objects.nonNull(singleFormAnswer.getCreatedTime())) {
            singleFormAnswer.setCreatedTime(new Date());
        }
        return mongoTemplate.insert(singleFormAnswer);
    }

    @Override
    public boolean deleteBySingleFormQuestionId(String singleFormQuestionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("singleFormQuestionId").is(singleFormQuestionId));
        Update update = new Update();
        update.set("deleted", true);
        update.set("updatedTime", new Date());
        return mongoTemplate.updateMulti(query, update, SingleFormAnswer.class).getModifiedCount() > 0 ? true : false;
    }

    @Override
    public boolean update(SingleFormAnswer s) {
        log.info(":>>> 开始更新答案:{}", JSON.toJSON(s));
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(s.getId()));
        Update update = new Update();
        boolean toUpdate = false;
        if (StringUtils.isNotBlank(s.getSingleFormQuestionId())) {
            update.set("singleFormQuestionId", s.getSingleFormQuestionId());
            toUpdate = true;
        }
        if (Objects.nonNull(s.getNo())) {
            update.set("no", s.getNo());
            toUpdate = true;
        }
        if (Objects.nonNull(s.isAnswer())) {
            update.set("isAnswer", s.isAnswer());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(s.getContent())) {
            update.set("content", s.getContent());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(s.getContentType())) {
            update.set("contentType", s.getContentType());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(s.getImageUrl())) {
            update.set("imageUrl", s.getImageUrl());
            toUpdate = true;
        }
        if (toUpdate) {
            update.set("updatedTime", new Date());
            return mongoTemplate.updateFirst(query, update, SingleFormAnswer.class).getModifiedCount() > 0 ? true : false;
        }
        return false;
    }

    @Override
    public SingleFormAnswer findOne(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, SingleFormAnswer.class);
    }
}
